package com.lgmshare.component.app;

import android.app.Activity;

import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.Stack;

/**
 * Activity管理栈
 */
public class LaraActivityManager {

    private final Stack<WeakReference<Activity>> mActivityStack = new Stack<WeakReference<Activity>>();

    private final static class HolderClass {
        private final static LaraActivityManager INSTANCE = new LaraActivityManager();
    }

    public static LaraActivityManager getInstance() {
        return HolderClass.INSTANCE;
    }

    /**
     * 检查弱引用是否释放，若释放，则从栈中清理掉该元素
     */
    public void checkWeakReference() {
        if (mActivityStack != null) {
            // 使用迭代器进行安全删除
            for (Iterator<WeakReference<Activity>> it = mActivityStack.iterator(); it.hasNext(); ) {
                WeakReference<Activity> activityReference = it.next();
                Activity temp = activityReference.get();
                if (temp == null) {
                    it.remove();
                }
            }
        }
    }

    /**
     * 获取当前Activity（栈中最后一个压入的[栈顶]）
     */
    public Activity getCurrentActivity() {
        checkWeakReference();
        if (mActivityStack != null && !mActivityStack.isEmpty()) {
            return mActivityStack.lastElement().get();
        }
        return null;
    }

    /**
     * 添加Activity到栈中
     */
    public void pushActivity(Activity activity) {
        if (activity != null) {
            mActivityStack.push(new WeakReference<Activity>(activity));
        }
    }

    /**
     * 移除指定的Activity
     */
    public void popActivity(Activity activity) {
        if (activity != null && mActivityStack != null) {
            // 使用迭代器进行安全删除
            for (Iterator<WeakReference<Activity>> it = mActivityStack.iterator(); it.hasNext(); ) {
                WeakReference<Activity> activityReference = it.next();
                Activity temp = activityReference.get();
                // 清理掉已经释放的activity
                if (temp == null) {
                    it.remove();
                    continue;
                }
                if (temp == activity) {
                    it.remove();
                }
            }
        }
    }

    /**
     * 结束当前Activity（栈中最后一个压入的[栈顶]）
     */
    public void finishCurrentActivity() {
        Activity activity = getCurrentActivity();
        if (activity != null) {
            finishActivity(activity);
        }
    }

    /**
     * 结束指定Activity
     *
     * @param activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null && mActivityStack != null) {
            // 使用迭代器进行安全删除
            for (Iterator<WeakReference<Activity>> it = mActivityStack.iterator(); it.hasNext(); ) {
                WeakReference<Activity> activityReference = it.next();
                Activity temp = activityReference.get();
                // 清理掉已经释放的activity
                if (temp == null) {
                    it.remove();
                    continue;
                }
                if (temp == activity) {
                    it.remove();
                }
            }
            activity.finish();
        }
    }

    /**
     * 结束指定类名的Activity
     *
     * @param clazz
     */
    public void finishActivity(Class<?> clazz) {
        if (mActivityStack != null) {
            // 使用迭代器进行安全删除
            for (Iterator<WeakReference<Activity>> it = mActivityStack.iterator(); it.hasNext(); ) {
                WeakReference<Activity> activityReference = it.next();
                Activity activity = activityReference.get();
                // 清理掉已经释放的activity
                if (activity == null) {
                    it.remove();
                    continue;
                }
                if (activity.getClass().equals(clazz)) {
                    it.remove();
                    activity.finish();
                }
            }
        }
    }

    /**
     * 结束此Activity在栈前的所有Activity,不包含此Activity
     *
     * @param clazz 标记activity
     */
    public void finishTopActivity(Class<?> clazz) {
        if (clazz == null) {
            return;
        }
        while (true) {
            Activity activity = getCurrentActivity();
            if (activity == null || activity.getClass().equals(clazz)) {
                break;
            }
            finishActivity(activity);
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        while (!mActivityStack.isEmpty()) {
            finishActivity(getCurrentActivity());
        }
        mActivityStack.clear();
    }
}
